<?php

namespace App\Listeners;

use App\Events\AdminLog\Log as EventsActionLog;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Models\Common\OperationLog as ActionLogModel;

class ActionLog implements ShouldQueue
{
    private $model;

    /**
     * Create the event handler.
     *
     * @return void
     */
    public function __construct()
    {
        $this->model = new ActionLogModel();
    }

    /**
     * Handle the event.
     *
     * @param  EventsActionLog $event
     * @return void
     */
    public function handle(EventsActionLog $event)
    {
        $distinct = config('system.action_log_scope');
        //判断1存在时情况
        $res = in_array(1,$distinct);
        if($res){
            $data[] = 1;
        }else{
            $data = $distinct;
        }
        //设置存储条件
        for ($i = 0; $i < count($data); $i++) {
            switch ($data[$i]) {
                case intval($data[$i]) == 1:
                    $this->addLog($event);
                    break;
                case intval($data[$i]) == 2:
                    if (request()->getRealMethod() == 'GET' && $event->message[0] == 0) {
                        $this->addLog($event);
                    }
                    break;
                case intval($data[$i]) == 3:
                    if (request()->getRealMethod() == 'POST' && $event->message[0] == 0) {
                        $this->addLog($event);
                    }
                    break;
                case intval($data[$i]) == 4:
                    if (request()->getRealMethod() == 'DELETE' && $event->message[0] == 0) {
                        $this->addLog($event);
                    }
                    break;
                case intval($data[$i]) == 5:
                    if ($event->message[0] != 0) {
                        $this->addLog($event);
                    }
                    break;
            }
        }
    }

    /**
     * 添加log
     * @param $event
     */
    protected function addLog($event)
    {
        try{
            $addDatas['user_id'] = $event->userId;
            if (!$addDatas['user_id']) {
                $addDatas['user_id'] = null;
            }
            $addDatas['ip'] = real_ip();
            $addDatas['method'] = request()->getRealMethod();
            if(strtolower($addDatas['method']) != 'options'){
                $addDatas['path'] = request()->getRequestUri();
                $addDatas['params'] = $event->params;
                $addDatas['description'] = $event->message[1];
//        $addDatas['type'] = isset($event->type) ? $event->type : 0;
                //设备型号
                $addDatas['client'] = isMobile() ? getMobileOs() : 'PC' . '(' . getPcOs() . ')';
                $addDatas['browser'] = isset($_SERVER['HTTP_USER_AGENT']) ? getAgentModel() : '';
                $this->model->add($addDatas);
            }
        }catch (\Exception $e){

        }

    }

    /**
     * 队列添加失败
     * @param EventsActionLog $event
     * @param $exception
     */
    public function failed(EventsActionLog $event, $exception)
    {
        //TODO:（可以任意选一个）1、可以发送邮件到指定邮箱记录;2、记录到本地log文件中;
        //TODO:3、创建数据表记录;4、回执
    }
}
